package com.kylin.module.baseapp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kylin.common.Constants;
import com.kylin.common.entity.MsgKey;
import com.kylin.module.baseapp.business.BaseBlogic;
import com.kylin.module.shop.forms.CompanyForm;
import com.kylin.module.user.orm.User;
import com.kylin.struts.BaseDispatchAction;

public class BaseAction extends BaseDispatchAction {

	protected static Logger log = Logger.getLogger(BaseAction.class);

	public ActionForward registerCompany(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始新增公司信息 : "+request.getParameter("name"));
		CompanyForm form = (CompanyForm) actionForm;
		String forward = "addSucc";
		try{
			form.setLanguage(getSessionLocale(request).toString());
			BaseBlogic bean = new BaseBlogic();
			bean.registerCompany(form);
			log.info("新增公司信息成功："+ form.getName());
		}catch (Exception e) {
			forward = doException( request, e, "addBusinessException");
		}
		return mapping.findForward(forward);
	}


	public ActionForward login(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("用户登录 : "+request.getParameter("userName"));
		String forward = "loginSucc";
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String language = this.getLocale(request).toString();
		try {
			BaseBlogic blogic = new BaseBlogic();
			User user = blogic.getLoginUser(userName, passWord,language);
			request.getSession().setAttribute(Constants.CURR_USER, user);
		}catch (Exception e) {
			forward = doException( request, e, "loginFail");
		}
		return mapping.findForward(forward);
	}

	public ActionForward adminLogin(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("管理员登录 : "+request.getParameter("userName"));
		String forward = "adminLoginSucc";
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		try {
			BaseBlogic blogic = new BaseBlogic();
			User user = blogic.getAdminLoginUser(userName, passWord);
			request.getSession().setAttribute("adminUser", user);
		}catch (Exception e) {
			forward = "adminLoginFail";
			doException( request, e, "adminLoginFail");
		}
		return mapping.findForward(forward);
	}

	public ActionForward forgetPW(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("用户: "+request.getParameter("userName"));
		String forward = "getPassword";
		String userName = request.getParameter("userName");
		String email = request.getParameter("email");
		try {
			BaseBlogic blogic = new BaseBlogic();
			blogic.sendLostPassword(userName, email);
			request.setAttribute(Constants.MSGKEY, new MsgKey("sendLostPassword.over","密码通知邮件已发送，请查收"));;
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}
	


}
